package org.example.quartz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.quartz.util.HttpUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class test {


    public static void main(String[] args) {
        JSONObject J = new JSONObject();
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        String msg = "";

        try {
            String url = "https://dd2.ekuaibao.com/api/openapi/v1/auth/getAccessToken";
            Map<String, String> params = new HashMap();
            params.put("appKey", "80897893-5555-4d20-8b59-8d0d960e7543");
            params.put("appSecurity", "c9b42656-a660-4d26-a190-650b9f23d9eb");
            String method = "POST";
            JSONObject json = HttpUtil.http(url, params, method);
            JSONObject tokenjson = JSON.parseObject(json.getString("value"));
            String token = tokenjson.getString("accessToken");


            String url1 = "https://dd2.ekuaibao.com/api/openapi/v1/docSequences";
            Map<String, String> params1 = new HashMap();
            params1.put("accessToken", token);
            params1.put("powerCode", "219902");
            params1.put("type", "expense");
            String method1;
            String json1;
            String indexTime;

                method1 = "2024-10-28 14:17:08:000000";
                json1 = "yyyy-MM-dd HH:mm:ss:SSSSSS";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(json1);
                Date date = simpleDateFormat.parse(method1);
                long ts = date.getTime();
                indexTime = String.valueOf(ts);
                params1.put("index", "1730182628584");


            params1.put("count", "1000");
            method1 = "GET";
            json1 = HttpUtil.doGet(url1, params1, method1);
            JSONObject bxJson = JSON.parseObject(json1);
            if (bxJson != null) {
                JSONArray bxList = JSON.parseArray(bxJson.getString("items"));
                if (bxList.size() > 0) {
                    for(int i = 0; i < bxList.size(); ++i) {
                        JSONObject obj = bxList.getJSONObject(i);
                        indexTime = obj.getString("index");
                        //System.out.println("获取到的时间戳是：" + indexTime);
                        JSONObject docData = JSON.parseObject(obj.getString("docData"));
                        System.out.println(docData);
                        insertBxdTable(indexTime,docData);

                    }
                } else {
                    msg = "当前无可同步的报销单数据。";
                }
            }
        } catch (Exception var30) {
            var30.printStackTrace();
            System.out.println(var30.toString());
        }
        J.put("msg", msg);

    }



    public static String insertBxdTable(String indexTime,JSONObject jsonobj) {
        Integer bxdid = 0;

        if (jsonobj.getString("code").equals("B24003570")) {
            System.out.println("跳過此數據");
        } else {




            //id 易快报主键id
            if (jsonobj.getString("id") == null || "".equals(jsonobj.getString("id"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //时间戳
            if (indexTime == null || "".equals(indexTime)) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //code  报销单编码
            if (jsonobj.getString("code") == null || "".equals(jsonobj.getString("code"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //corporationId  单位id
            if (jsonobj.getString("corporationId") == null || "".equals(jsonobj.getString("corporationId"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //remark  备注
            if (jsonobj.getString("remark") == null || "".equals(jsonobj.getString("remark"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //type 类型
            if (jsonobj.getString("type") == null || "".equals(jsonobj.getString("type"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }

            //title  标题
            if (jsonobj.getString("title") == null || "".equals(jsonobj.getString("title"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //sumAmount  总金额
            if (jsonobj.getString("sumAmount") == null || "".equals(jsonobj.getString("sumAmount"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //state  报销状态
            if (jsonobj.getString("state") == null || "".equals(jsonobj.getString("state"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //payorId   支付ID
            if (jsonobj.getString("payorId") == null || "".equals(jsonobj.getString("payorId"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //支付时间
            if (jsonobj.getString("payTime") == null || "".equals(jsonobj.getString("payTime"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //specificationId   规格id
            if (jsonobj.getString("specificationId") == null || "".equals(jsonobj.getString("specificationId"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //dataType  数据类型
            if (jsonobj.getString("dataType") == null || "".equals(jsonobj.getString("dataType"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //updateTime   修改时间
            if (jsonobj.getString("updateTime") == null || "".equals(jsonobj.getString("updateTime"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //expenseDate  报销日期
            if (jsonobj.getString("expenseDate") == null || "".equals(jsonobj.getString("expenseDate"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //submitTime   提交日期
            if (jsonobj.getString("submitTime") == null || "".equals(jsonobj.getString("submitTime"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }

            //获取人员obj数据
            JSONObject owner = JSON.parseObject(jsonobj.getString("owner"));
            //人员id
            if (owner.getString("id") == null || "".equals(owner.getString("id"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //人员名称
            if (owner.getString("name") == null || "".equals(owner.getString("name"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //获取部门信息
            JSONObject dept = JSON.parseObject(jsonobj.getString("department"));
            //部门id
            if (dept.getString("id") == null || "".equals(dept.getString("id"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //部门名称
            if (dept.getString("name") == null || "".equals(dept.getString("name"))) {
                System.out.println("============================");
            } else {
                System.out.println("============================");
            }
            //payeeInfo信息
            JSONObject payeeInfo = JSON.parseObject(jsonobj.getString("payeeInfo"));
            if (payeeInfo != null) {
                //支付单位名称
                if (payeeInfo.getString("name") == null || "".equals(payeeInfo.getString("name"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //卡号
                if (payeeInfo.getString("cardNo") == null || "".equals(payeeInfo.getString("cardNo"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //支付银行
                if (payeeInfo.getString("bank") == null || "".equals(payeeInfo.getString("bank"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //支付银行全称
                if (payeeInfo.getString("branch") == null || "".equals(payeeInfo.getString("branch"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //支付类型
                if (payeeInfo.getString("type") == null || "".equals(payeeInfo.getString("type"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //支付省份
                if (payeeInfo.getString("province") == null || "".equals(payeeInfo.getString("province"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //支付城市
                if (payeeInfo.getString("city") == null || "".equals(payeeInfo.getString("city"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
            }

//            BxdTableDao.insertBxdTable(bxdTable1);
//            bxdid = bxdTable1.getBxdid();

            //插入子表数据
            JSONArray details = JSON.parseArray(jsonobj.getString("details"));

            for (int x = 0; x < details.size(); x++) {
                //获取单条报销明细
                // BxdDetail bxdDetail = new BxdDetail();
                JSONObject detailObj = details.getJSONObject(x);


                //外键id
                if (bxdid == null || "".equals(bxdid)) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //amount  金额
                if (detailObj.getString("amount") == null || "".equals(detailObj.getString("amount"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //币种编码
                if (detailObj.getString("amount_standardStrCode") == null || "".equals(detailObj.getString("amount_standardStrCode"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //默认编码
                if (detailObj.getString("amount_standardNumCode") == null || "".equals(detailObj.getString("amount_standardNumCode"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //符号
                if (detailObj.getString("amount_standardSymbol") == null || "".equals(detailObj.getString("amount_standardSymbol"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //计量单位
                if (detailObj.getString("amount_standardUnit") == null || "".equals(detailObj.getString("amount_standardUnit"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //汇率
                if (detailObj.getString("amount_rate") == null || "".equals(detailObj.getString("amount_rate"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //日期时间戳
                if (detailObj.getString("date") == null || "".equals(detailObj.getString("date"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }
                //费用类型id
                if (detailObj.getString("feeTypeId") == null || "".equals(detailObj.getString("feeTypeId"))) {
                    System.out.println("============================");
                } else {
                    System.out.println("============================");
                }

            }


        }
        return bxdid.toString();
        }



}
